const {AlipaySdk} = require("alipay-sdk");
const {AliPayConfig} = require("../../config/AuthConfig");
const alipaySdk = new AlipaySdk({
    appId: AliPayConfig.appId,
    privateKey: AliPayConfig.privateKey,
    alipayPublicKey: AliPayConfig.alipayPublicKey,
    gateway: AliPayConfig.gateway,
});
exports.Alipay = async (req, res, next) => {
    const {orderId = 'order_' + Date.now(), amount = '0.01'} = req.query;
    try {
        const result = await alipaySdk.exec('alipay.trade.precreate', {
            bizContent: {
                out_trade_no: orderId,
                total_amount: amount,
                subject: '电影票支付',
            },
        });
        console.log(result)
        if (result.code === '10000') {
            res.json({code: 0, traceId: result.outTradeNo, qr: result.qrCode});
        } else {
            res.json({code: 1, msg: result.sub_msg});
        }
    } catch (err) {
        console.error('支付宝生成二维码失败：');
        res.json({code: 1, msg: '支付宝下单异常'});
    }
}
exports.AlipayNotify = async (req, res, next) => {
    const {orderId} = req.params;
    try {
        const result = await alipaySdk.exec('alipay.trade.query', {
            bizContent: {
                out_trade_no: orderId,
            },
        });
        console.log(result)
        console.log(result.code, result.msg);
        res.json({
            trade_status: result.tradeStatus,
            success: result.tradeStatus === 'TRADE_SUCCESS',
        });
    } catch (err) {
        console.error('查询支付状态失败：');
        res.json({success: false, msg: '查询失败'});
    }
}